package org.travel.app.service.impl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.travel.app.common.myenum.ReservationType;
import org.travel.app.entity.BaseReservation;
import org.travel.app.entity.HotelReservation;
import org.travel.app.entity.Order;
import org.travel.app.entity.spots.SpotsReservation;
import org.travel.app.mapper.ReservationMapper;
import org.travel.app.service.ReservationService;

/**
 * @author nmm
 * @date 2024-12-2024/12/10
 * @deriction  预约表信息添加
 */
@Service
@RequiredArgsConstructor
public class ReservationImpl implements ReservationService {
private final ReservationMapper reservationMapper;
private final HotelServiceImpl hotelService;
private final SpotsServiceImpl spotsService;
    @Override
    public String saveReservation(BaseReservation reservation) {
        String orderId="";
        //添加预约信息
        //如果是预约类型为酒店则进行酒店预约信息的添加
            //如果属于酒店预约
            if (reservation instanceof HotelReservation hotelReservation){
                saveAndSetReservation(hotelReservation,ReservationType.HOTEL);
                //则转换成 HotelReservation 实例
                hotelService.addHotelReservation(hotelReservation);
            }else {
                SpotsReservation spotsReservation = (SpotsReservation) reservation;
                saveAndSetReservation(spotsReservation,ReservationType.SPOT);
                //添加预约
                orderId= spotsService.addSpotsReservation(spotsReservation);
                //添加景点预约信息
            }
         return orderId;
    }
    
    /**
     * 添加预约信息
      * @param reservation
     * @param type
     */
    public void saveAndSetReservation(BaseReservation reservation,ReservationType type) {
        reservation.setReservationType(type);
        reservationMapper.saveReservation(reservation);
    }

    /**
     * 通过订单号返回订单及其门票相关信息
     * @param orderId
     * @return
     */
    @Override
    public Order getSpotsReservationByOrderId(String orderId) {
       return reservationMapper.getSpotsReservationByOrderId(orderId);
    }

}